<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>梦幻电子琴</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #f5f7fa 0%, #ffeef8 100%);
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
            color: #5a5a5a;
            position: relative;
            overflow-x: hidden;
        }

        /* 浮动气泡背景 */
        .bubble {
            position: absolute;
            border-radius: 50%;
            background: rgba(255, 182, 193, 0.2);
            animation: float 15s infinite linear;
            z-index: -1;
        }

        @keyframes float {
            0% { transform: translateY(0) rotate(0deg); }
            100% { transform: translateY(-1000px) rotate(720deg); }
        }

        .back-button {
            position: absolute;
            left: 30px;
            top: 30px;
            padding: 12px 25px;
            background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%);
            color: white;
            text-decoration: none;
            border-radius: 50px;
            transition: all 0.3s;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            border: none;
            font-weight: bold;
            display: flex;
            align-items: center;
            gap: 5px;
            z-index: 10;
        }

        .back-button:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
        }

        .back-button:active {
            transform: translateY(1px);
        }

        h1 {
            color: #ff6b81;
            margin: 40px 0 20px;
            font-size: 2.5em;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
            font-family: 'Comic Sans MS', cursive;
            letter-spacing: 2px;
        }

        .subtitle {
            color: #ff8fab;
            margin-bottom: 30px;
            font-size: 1.2em;
            text-align: center;
        }

        .piano-container {
            position: relative;
            margin: 30px 0 50px;
            padding: 30px;
            background: rgba(255, 255, 255, 0.9);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(255,105,180,0.2);
            transition: all 0.3s;
            width: 90%;
            max-width: 1200px;
            overflow: hidden;
        }

        .piano-container:hover {
            box-shadow: 0 20px 40px rgba(255,105,180,0.3);
        }

        .piano {
            display: flex;
            position: relative;
            height: 300px;
        }

        .white-key {
            position: relative;
            width: 80px;
            height: 100%;
            background: linear-gradient(to bottom, #fff 0%, #f9f9f9 100%);
            border: 1px solid #ffd6e7;
            border-radius: 0 0 10px 10px;
            box-shadow: 2px 5px 10px rgba(0,0,0,0.1);
            transition: all 0.1s;
            z-index: 1;
            display: flex;
            justify-content: center;
            align-items: flex-end;
            padding-bottom: 15px;
        }

        .white-key.active {
            background: linear-gradient(to bottom, #ffe4e1 0%, #ffd6e7 100%);
            box-shadow: inset 0 0 15px rgba(255,105,180,0.3);
            transform: translateY(2px);
        }

        .black-key {
            position: absolute;
            width: 50px;
            height: 60%;
            background: linear-gradient(145deg, #333 0%, #222 100%);
            border-radius: 0 0 8px 8px;
            box-shadow: 3px 5px 15px rgba(0,0,0,0.4);
            z-index: 2;
            transition: all 0.1s;
            display: flex;
            justify-content: center;
            align-items: flex-end;
            padding-bottom: 15px;
        }

        .black-key.active {
            background: linear-gradient(145deg, #222 0%, #111 100%);
            box-shadow: inset 0 0 15px rgba(0,0,0,0.5);
            transform: translateY(2px);
        }

        .key-label {
            font-size: 18px;
            font-weight: bold;
            user-select: none;
        }

        .white-key .key-label {
            color: #ff6b81;
        }

        .black-key .key-label {
            color: #fff;
        }

        .controls {
            margin: 20px 0;
            padding: 15px 30px;
            background: rgba(255,255,255,0.9);
            border-radius: 50px;
            color: #ff6b81;
            font-size: 1em;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            text-align: center;
            max-width: 80%;
        }

        .keyboard-hint {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 15px;
            margin-top: 20px;
        }

        .key-hint {
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .key-hint-key {
            width: 40px;
            height: 40px;
            background: linear-gradient(145deg, #ff9aa2 0%, #ff6b81 100%);
            color: white;
            border-radius: 8px;
            display: flex;
            justify-content: center;
            align-items: center;
            font-weight: bold;
            margin-bottom: 5px;
            box-shadow: 0 4px 8px rgba(255,105,180,0.3);
        }

        .key-hint-note {
            font-size: 12px;
            color: #ff6b81;
        }

        .visualizer {
            width: 100%;
            height: 100px;
            margin-top: 30px;
            background: rgba(255,255,255,0.7);
            border-radius: 10px;
            overflow: hidden;
            position: relative;
        }

        .wave {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(to right, #ff9aa2, #ffb7b2);
            opacity: 0.3;
            transform-origin: bottom;
        }

        @media (max-width: 768px) {
            .piano-container {
                padding: 15px;
                width: 95%;
            }

            .white-key {
                width: 40px;
            }

            .black-key {
                width: 30px;
            }

            .key-label {
                font-size: 14px;
            }

            .controls {
                font-size: 0.9em;
                padding: 10px 20px;
            }

            h1 {
                font-size: 2em;
                margin-top: 60px;
            }

            .back-button {
                left: 15px;
                top: 15px;
                padding: 8px 15px;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <a href="/game/" class="back-button">← 返回</a>

    <h1>梦幻电子琴</h1>
    <div class="subtitle">用键盘或点击琴键演奏美妙音乐</div>

    <div class="piano-container">
        <div class="piano" id="piano"></div>
        <div class="visualizer" id="visualizer">
            <div class="wave" id="wave"></div>
        </div>
    </div>

    <div class="controls">
        <div>使用键盘 A-L 演奏白键，W-U 演奏黑键</div>
        <div class="keyboard-hint" id="keyboardHint"></div>
    </div>

    <script>
        // 创建背景气泡
        function createBubbles() {
            const bubbleCount = 15;
            for (let i = 0; i < bubbleCount; i++) {
                const bubble = document.createElement('div');
                bubble.classList.add('bubble');

                const size = Math.random() * 100 + 50;
                const posX = Math.random() * window.innerWidth;
                const delay = Math.random() * 15;
                const duration = Math.random() * 20 + 10;

                bubble.style.width = `${size}px`;
                bubble.style.height = `${size}px`;
                bubble.style.left = `${posX}px`;
                bubble.style.bottom = `-${size}px`;
                bubble.style.animationDelay = `${delay}s`;
                bubble.style.animationDuration = `${duration}s`;

                document.body.appendChild(bubble);
            }
        }

        createBubbles();

        const notes = [
            { key: 'a', note: 'C4', type: 'white', label: 'A', name: 'Do' },
            { key: 'w', note: 'C#4', type: 'black', label: 'W', name: 'Do#' },
            { key: 's', note: 'D4', type: 'white', label: 'S', name: 'Re' },
            { key: 'e', note: 'D#4', type: 'black', label: 'E', name: 'Re#' },
            { key: 'd', note: 'E4', type: 'white', label: 'D', name: 'Mi' },
            { key: 'f', note: 'F4', type: 'white', label: 'F', name: 'Fa' },
            { key: 't', note: 'F#4', type: 'black', label: 'T', name: 'Fa#' },
            { key: 'g', note: 'G4', type: 'white', label: 'G', name: 'Sol' },
            { key: 'y', note: 'G#4', type: 'black', label: 'Y', name: 'Sol#' },
            { key: 'h', note: 'A4', type: 'white', label: 'H', name: 'La' },
            { key: 'u', note: 'A#4', type: 'black', label: 'U', name: 'La#' },
            { key: 'j', note: 'B4', type: 'white', label: 'J', name: 'Si' },
            { key: 'k', note: 'C5', type: 'white', label: 'K', name: 'Do' }
        ];

        const frequencies = {
            'C4': 261.63, 'C#4': 277.18, 'D4': 293.66, 'D#4': 311.13,
            'E4': 329.63, 'F4': 349.23, 'F#4': 369.99, 'G4': 392.00,
            'G#4': 415.30, 'A4': 440.00, 'A#4': 466.16, 'B4': 493.88,
            'C5': 523.25
        };

        // 初始化音频上下文
        let audioContext;
        try {
            audioContext = new (window.AudioContext || window.webkitAudioContext)();
        } catch (e) {
            alert('您的浏览器不支持Web Audio API，请使用现代浏览器如Chrome、Firefox或Edge');
        }

        const piano = document.getElementById('piano');
        const keyboardHint = document.getElementById('keyboardHint');
        const wave = document.getElementById('wave');

        // 创建键盘提示
        notes.forEach(note => {
            const hint = document.createElement('div');
            hint.className = 'key-hint';

            const keyElement = document.createElement('div');
            keyElement.className = 'key-hint-key';
            keyElement.textContent = note.key.toUpperCase();

            const noteElement = document.createElement('div');
            noteElement.className = 'key-hint-note';
            noteElement.textContent = note.name;

            hint.appendChild(keyElement);
            hint.appendChild(noteElement);
            keyboardHint.appendChild(hint);
        });

        // 创建琴键
        let blackKeyCount = 0;
        notes.forEach((note, index) => {
            if (note.type === 'white') {
                const key = document.createElement('div');
                key.className = 'white-key';
                key.dataset.note = note.note;
                key.dataset.key = note.key;

                const label = document.createElement('div');
                label.className = 'key-label';
                label.textContent = note.label;

                key.appendChild(label);
                piano.appendChild(key);

                // 重置黑键计数
                blackKeyCount = 0;
            } else {
                const key = document.createElement('div');
                key.className = 'black-key';
                key.dataset.note = note.note;
                key.dataset.key = note.key;

                // 计算黑键位置 (每两个白键后位置重置)
                const left = 80 * (index - blackKeyCount) - 25;
                key.style.left = `${left}px`;

                const label = document.createElement('div');
                label.className = 'key-label';
                label.textContent = note.label;

                key.appendChild(label);
                piano.appendChild(key);

                blackKeyCount++;
            }
        });

        // 播放音符
        function playNote(frequency) {
            if (!audioContext) return;

            const oscillator = audioContext.createOscillator();
            const gainNode = audioContext.createGain();

            // 使用更丰富的音色
            oscillator.type = 'triangle';
            oscillator.frequency.setValueAtTime(frequency, audioContext.currentTime);

            // 更自然的音量包络
            gainNode.gain.setValueAtTime(0, audioContext.currentTime);
            gainNode.gain.linearRampToValueAtTime(0.3, audioContext.currentTime + 0.03);
            gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 1);

            oscillator.connect(gainNode);
            gainNode.connect(audioContext.destination);

            oscillator.start();
            oscillator.stop(audioContext.currentTime + 1);

            // 可视化效果
            animateWave(frequency);
        }

        // 波形动画
        function animateWave(frequency) {
            const scale = frequency / 523.25; // 基于最高频率的比例
            wave.style.transform = `scaleY(${0.5 + scale * 0.5})`;
            wave.style.opacity = '0.5';

            setTimeout(() => {
                wave.style.transform = 'scaleY(0.1)';
                wave.style.opacity = '0.2';
            }, 200);
        }

        // 处理演奏事件
        function handlePlay(event) {
            const key = event.target.closest('.white-key, .black-key');
            if (!key) return;

            key.classList.add('active');
            const note = key.dataset.note;
            playNote(frequencies[note]);

            // 添加按下效果
            key.style.transform = key.classList.contains('white-key') ?
                'translateY(2px)' : 'translateY(1px)';
        }

        function handleRelease(event) {
            const key = event.target.closest('.white-key, .black-key');
            if (key) {
                key.classList.remove('active');
                key.style.transform = '';
            }
        }

        // 添加事件监听器
        piano.addEventListener('mousedown', handlePlay);
        document.addEventListener('mouseup', handleRelease);
        piano.addEventListener('touchstart', e => {
            e.preventDefault();
            handlePlay(e.touches[0]);
        });
        document.addEventListener('touchend', handleRelease);

        document.addEventListener('keydown', e => {
            if (e.repeat) return;
            const key = document.querySelector(`[data-key="${e.key.toLowerCase()}"]`);
            if (key) {
                key.classList.add('active');
                key.style.transform = key.classList.contains('white-key') ?
                    'translateY(2px)' : 'translateY(1px)';
                playNote(frequencies[key.dataset.note]);
            }
        });

        document.addEventListener('keyup', e => {
            const key = document.querySelector(`[data-key="${e.key.toLowerCase()}"]`);
            if (key) {
                key.classList.remove('active');
                key.style.transform = '';
            }
        });

        // 点击提示演示
        keyboardHint.addEventListener('click', (e) => {
            const hintKey = e.target.closest('.key-hint-key');
            if (hintKey) {
                const keyChar = hintKey.textContent.toLowerCase();
                const key = document.querySelector(`[data-key="${keyChar}"]`);
                if (key) {
                    key.classList.add('active');
                    key.style.transform = key.classList.contains('white-key') ?
                        'translateY(2px)' : 'translateY(1px)';
                    playNote(frequencies[key.dataset.note]);

                    setTimeout(() => {
                        key.classList.remove('active');
                        key.style.transform = '';
                    }, 300);
                }
            }
        });
    </script>
</body>
</html>